﻿using System.Web.Mvc;
using System.Linq;
using Mvc4Demo.Extensions;

namespace Mvc4Demo.Core
{
    internal class MobileViewEngine : RazorViewEngine
    {
        private const string vbhtmlExtention = "vbhtml";
        public MobileViewEngine()
        {
            //remove VB file exts.
            FileExtensions = FileExtensions.Where(x => !x.ToLower().Contains(vbhtmlExtention)).ToArray();
            MasterLocationFormats = MasterLocationFormats.Where(x => !x.ToLower().Contains(vbhtmlExtention)).ToArray();
            ViewLocationFormats = ViewLocationFormats.Where(x => !x.ToLower().Contains(vbhtmlExtention)).ToArray();
            PartialViewLocationFormats = PartialViewLocationFormats.Where(x => !x.ToLower().Contains(vbhtmlExtention)).ToArray(); ;
            AreaViewLocationFormats = AreaViewLocationFormats.Where(x => !x.ToLower().Contains(vbhtmlExtention)).ToArray(); ;
            AreaPartialViewLocationFormats = AreaPartialViewLocationFormats.Where(x => !x.ToLower().Contains(vbhtmlExtention)).ToArray(); ;
            AreaMasterLocationFormats = AreaMasterLocationFormats.Where(x => !x.ToLower().Contains(vbhtmlExtention)).ToArray(); ;
            var mobileModel = DisplayModeProvider.Modes.FirstOrDefault(x => x.DisplayModeId.ToLower() == "mobile");
            if (mobileModel != null)
            {
                DisplayModeProvider.Modes.Remove(mobileModel);
            }
        }

        public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
        {
            ViewEngineResult result = null;
            var request = controllerContext.HttpContext.Request;
            string deviceType;
            if (HttpExtensions.HasMobileSpecificViews && request.IsSupportedMobileDevice(out deviceType))
            {
                var viewPathAndName = HttpExtensions.ViewsInFolder
                                          ? string.Format("{0}/{1}", deviceType, viewName)
                                          : string.Format("{0}.{1}", deviceType, viewName);

                result = base.FindView(controllerContext, viewPathAndName, masterName, true);

                if (result == null || result.View == null)
                {
                    result = base.FindView(controllerContext, viewPathAndName, masterName, false);
                }
            }
            else
            {
                result = base.FindView(controllerContext, viewName, masterName, useCache);
            }
            return result;
        }

        protected override bool FileExists(ControllerContext controllerContext, string virtualPath)
        {
            return base.FileExists(controllerContext, virtualPath);
        }

        public override ViewEngineResult FindPartialView(ControllerContext controllerContext, string partialViewName, bool useCache)
        {
            return base.FindPartialView(controllerContext, partialViewName, useCache);
        }
    }
}
